<template>
  <div class="login" :class="[setTheme == 1 ? 'blue' : 'red']">
    <div class="login-form">
      <i class="el-icon-close close" @click="handleClose"></i>
      <h3 class="title">{{ $t("langName.langName1") }}</h3>
      <el-form
        :model="userForm"
        :rules="loginRules"
        ref="ruleForm"
        class="mt-80"
        :label-width="languageType == 1 ? '170px' : '140px'"
      >
        <!-- 账号 -->
        <el-form-item :label="$t('langName.langName11')" prop="nickname">
          <el-input
            v-model="userForm.nickname"
            :placeholder="$t('langName.langName16')"
          >
            <i slot="prefix" class="el-icon-s-custom img-size"></i
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item :label="$t('langName.langName12')" prop="password">
          <el-input
            v-model="userForm.password"
            type="password"
            :placeholder="$t('langName.langName17')"
            :show-password="true"
          >
            <i slot="prefix" class="el-icon-unlock img-size"></i>
          </el-input>
        </el-form-item>
        <!-- 确认密码 -->
        <el-form-item :label="$t('langName.langName13')" prop="nextPassword">
          <el-input
            v-model="userForm.nextPassword"
            type="password"
            :placeholder="$t('langName.langName18')"
            :show-password="true"
          >
            <i slot="prefix" class="el-icon-unlock img-size"></i>
          </el-input>
        </el-form-item>

        <el-button
          :type="setTheme == 1 ? 'primary' : 'danger'"
          class="submitBtn"
          @click="handleUpdate('ruleForm')"
          >{{ $t("langName.langName58") }}</el-button
        >
      </el-form>
    </div>
  </div>
</template>

<script>
import { forgotPassword } from "@/api/collect";
import Cookies from "js-cookie";
export default {
  data() {
    const validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error(this.$t("langName.langName16")));
      } else {
        if (this.userForm.nextPassword !== "") {
          this.$refs.ruleForm.validateField("nextPassword");
        }
        callback();
      }
    };

    const validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error(this.$t("langName.langName18")));
      } else if (value != this.userForm.password) {
        callback(new Error(this.$t("langName.langName34")));
      } else {
        callback();
      }
    };
    return {
      userForm: {
        nickname: "",
        password: "",
        nextPassword: "",
      },

      loginRules: {
        nickname: [
          {
            required: true,
            trigger: "blur",
            message: this.$t("langName.langName16"),
          },
        ],
        password: [
          {
            required: true,
            message: this.$t("langName.langName17"),
            trigger: "blur",
          },
          {
            min: 6,
            max: 15,
            message: "长度在 6 到 15 个字符",
            trigger: "blur",
          },
          { validator: validatePass, trigger: "blur" },
        ],
        nextPassword: [
          {
            required: true,
            message: this.$t("langName.langName18"),
            trigger: "blur",
          },
          {
            min: 6,
            max: 15,
            message: "长度在 6 到 15 个字符",
            trigger: "blur",
          },
          { validator: validatePass2, trigger: "blur", required: true },
        ],
      },
      languageType: Cookies.get("lang"),
    };
  },
  created() {
    this.userForm.nickname = this.$route.params.userName;
    this.setTheme = localStorage.getItem("setTheme")
      ? localStorage.getItem("setTheme")
      : "1";
  },

  methods: {
    // 关闭注册页
    handleClose() {
      this.$router.go(-1);
    },

    // 修改
    async handleUpdate(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          let params = {
            nickname: this.userForm.nickname,
            password: this.userForm.password,
          };
          let { status, message } = await forgotPassword(params);
          if (status == 200) {
            this.$message.success(this.$t("langName.langName35"));
            this.$router.go(-1);
          } else {
            this.$message.error(message);
          }
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
@import "../assets/css/login.css";
</style>
